//
//  GQFogetController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/5.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQFogetController.h"
#import "GQSetPwdController.h"
#import "TipView.h"
#import "GQRegisterCPwdParam.h"

#import <SMS_SDK/SMS_SDK.h>

#define kLoginTextHeight 44
#define kLoginTextMargin 10
#define kLoginViewX 0
#define kLoginViewY 20

@interface GQFogetController ()

@property (strong, nonatomic) UITextField *phoneNumber;
@property (strong, nonatomic) UITextField *verifyNumber;
@property (strong, nonatomic) UIView *phoneView;
@property (strong, nonatomic) UIButton *nextBtn;
@property (strong, nonatomic) NSTimer *timer;
@property (strong, nonatomic) UIButton *vertifyCodeLabel;
@property (assign, nonatomic) NSInteger count;

@end

@implementation GQFogetController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:236 / 255.0 green:236 / 255.0 blue:236 / 255.0 alpha:1.0];
    self.view.frame = CGRectMake(0, kNavigatationBarHeight, kScreenWidth, kScreenHeight - kNavigatationBarHeight);
    [self setUpPhoneVertifyView];
    [self setUpNextBtn];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pwdTextFieldDidChange) name:UITextFieldTextDidChangeNotification object:self.pwdNumber];
    
}

- (void)backItemDidClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 获取验证码
- (void)setUpPhoneVertifyView
{
    CGFloat VertifyCodeBtnWidth = 90;
    CGFloat VertifyCodeBtnHeight = 30;
    
    //手机号码输入框
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(kLoginViewX, kLoginViewY + kNavigatationBarHeight, kScreenWidth, 90)];
    
    UIImageView *phoneImage =[[UIImageView alloc] initWithImage:[UIImage imageWithOriginalName:@"login_mobile"]];
    phoneImage.frame  =CGRectMake(kLoginTextMargin, 4.5, 35, 35);
    [view addSubview:phoneImage];
    
    UITextField *phoneNumber = [[UITextField alloc] initWithFrame:CGRectMake(kLoginTextMargin + phoneImage.width, 0, kScreenWidth - kLoginTextMargin * 2 - VertifyCodeBtnWidth - phoneImage.width, kLoginTextHeight)];
    phoneNumber.backgroundColor = [UIColor whiteColor];
    phoneNumber.clearButtonMode = UITextFieldViewModeWhileEditing;
    phoneNumber.placeholder = @"手机号码";
    phoneNumber.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    self.phoneNumber = phoneNumber;
    view.backgroundColor = [UIColor whiteColor];
    
    //获取验证码
    UIButton *vertifyCodeLabel = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - kLoginTextMargin - VertifyCodeBtnWidth, phoneNumber.center.y - VertifyCodeBtnHeight / 2, VertifyCodeBtnWidth, VertifyCodeBtnHeight)];
    [vertifyCodeLabel setTitle:@"获取验证码" forState:UIControlStateNormal];
    vertifyCodeLabel.titleLabel.font = [UIFont systemFontOfSize:13];
    [vertifyCodeLabel.layer setMasksToBounds:YES];
    [vertifyCodeLabel.layer setCornerRadius:4];
    [vertifyCodeLabel.layer setBorderWidth:1];
    [vertifyCodeLabel setTitleColor:kLoginBtnColor forState:UIControlStateNormal];
    [vertifyCodeLabel addTarget:self action:@selector(getVertifyBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    self.vertifyCodeLabel = vertifyCodeLabel;
    
    
    //中间的线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(kLoginTextMargin, 45, (kScreenWidth - kLoginTextMargin * 2), 1)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:lineView];
    
    
    UIImageView *vertifyImage =[[UIImageView alloc] initWithImage:[UIImage imageWithOriginalName:@"login_lock"]];
    vertifyImage.frame  =CGRectMake(kLoginTextMargin, 50.5, 35, 35);
    [view addSubview:vertifyImage];
    
    UITextField *verifyNumber = [[UITextField alloc] initWithFrame:CGRectMake(kLoginTextMargin + vertifyImage.width, 46, kScreenWidth - kLoginTextMargin * 2 - vertifyImage.width, kLoginTextHeight)];
    verifyNumber.backgroundColor = [UIColor whiteColor];
    verifyNumber.placeholder = @"验证码";
    verifyNumber.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    
    //获取验证码
    UIButton *btnAccessEye = [[UIButton alloc] init];
    btnAccessEye.width = 20;
    btnAccessEye.height = 20;
    btnAccessEye.x = kScreenWidth - kLoginTextMargin - btnAccessEye.width;
    btnAccessEye.y = verifyNumber.center.y - btnAccessEye.width / 2;
    btnAccessEye.backgroundColor = [UIColor greenColor];
    
    
    self.verifyNumber = verifyNumber;
    [view addSubview:phoneNumber];
    [view addSubview:verifyNumber];
    [view addSubview:btnAccessEye];
    [view addSubview: vertifyCodeLabel];
    [self.view addSubview:view];
    self.phoneView = view;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vertifyTextFieldDidChange) name:UITextFieldTextDidChangeNotification object:self.verifyNumber];
    
    
}

- (void)vertifyTextFieldDidChange
{
    NSString *regex2 = @"^[0-9]{4}$";
    NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex2];
    BOOL isMatch2 = [pred2 evaluateWithObject:self.verifyNumber.text];

    if (isMatch2) {
        self.nextBtn.enabled = YES;
        self.nextBtn.backgroundColor = kLoginBtnColor;
    }else{
        self.nextBtn.enabled = NO;
        [self.nextBtn setBackgroundColor:[UIColor colorWithRed:138/255.0 green:148/255.0 blue:158/255.0 alpha:1.0]];;
    }
    
}

- (void)getVertifyBtnDidClick:(UIButton *)btn
{
    
    //验证手机号码是否正确
    NSString *regex1 = @"^1+[3578]+\\d{9}";
    NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex1];
    BOOL isMatch1 = [pred1 evaluateWithObject:self.phoneNumber.text];
    if (!isMatch1) {
        [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_sad"] tipText:@"手机号码不正确"];
        return;
    }
    
    //获取验证码
    [SMS_SDK getVerificationCodeBySMSWithPhone:self.phoneNumber.text zone:@"86" result:^(SMS_SDKError *error) {
        
    }];
    
    _count = 60;
    NSTimer *myTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    self.timer = myTimer;
    NSRunLoop *runLoop = [NSRunLoop mainRunLoop];
    [runLoop addTimer:myTimer forMode:NSRunLoopCommonModes];
    btn.userInteractionEnabled = NO;
    [btn setBackgroundColor:[UIColor colorWithRed:18 / 255.0 green:38 / 255.0 blue:58 / 255.0 alpha:0.4]];
}

- (void)setUpNextBtn
{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(kLoginTextMargin, CGRectGetMaxY(self.phoneView.frame) + kLoginViewY, (kScreenWidth - kLoginTextMargin * 2), 40)];
    [btn setTitle:@"下一步" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = 5;
    btn.enabled = NO;
    [btn setBackgroundColor:[UIColor colorWithRed:138/255.0 green:148/255.0 blue:158/255.0 alpha:1.0]];
    [btn addTarget:self action:@selector(nextBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    self.nextBtn = btn;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 点击下一步按钮
NSInteger count = 0;
- (void)nextBtnClick
{
    
    //验证手机号码
    NSString *regex1 = @"^1+[3578]+\\d{9}";
    NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex1];
    BOOL isMatch1 = [pred1 evaluateWithObject:self.phoneNumber.text];
    if (!isMatch1) {
        [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_sad"] tipText:@"手机号码不正确"];
        return;
    }
    
    //校验验证码
    [SMS_SDK commitVerifyCode:self.verifyNumber.text result:^(enum SMS_ResponseState state) {
        if (state) {
            [TipView showToView:self.view tipImage:[UIImage imageNamed:@"login_sad"] tipText:@"验证码错误"];
            return;
        }else{
            
            [GQRegisterCPwdParam sharedGQRegisterCPwdParam].account = [self.phoneNumber.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            GQSetPwdController *GQSPC = [[GQSetPwdController alloc] init];
            [self.navigationController pushViewController:GQSPC animated:YES];
        }
    }];

    
}

- (void)countDown
{
    [self.vertifyCodeLabel setTitle:[NSString stringWithFormat:@"%ld秒后获取", _count] forState:UIControlStateNormal];
    _count--;
    if (_count == -1) {
        [self.timer invalidate];
        self.timer = nil;
        self.vertifyCodeLabel.userInteractionEnabled = YES;
        self.vertifyCodeLabel.backgroundColor = [UIColor whiteColor];
        [self.vertifyCodeLabel setTitle:@"获取验证码" forState:UIControlStateNormal];
        
    }
}

@end
